<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>

            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 右侧操作按钮 -->
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="hAssign(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页组件框样式 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageParams.pagesize"
                @current-change="currentChangeFn"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <!-- :title="isEdit?'编辑':'新增'" -->
      <el-dialog
        :title="cTitle"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog = false">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        title="分配权限"
        :close-on-click-modal="false"
        :visible.sync="showDialogAssign"
      >
        <assignPermission
          v-if="showDialogAssign"
          :role-id="roleId"
          @close="showDialogAssign=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/settings'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },

  data() {
    return {
      roleId: '',
      activeName: 'first',
      showDialog: false, // 控制弹层
      showDialogAssign: false, // 分配权限弹层
      roleForm: {
        // 表单数据
        name: '',
        description: ''
      },
      isEdit: false,
      rules: {
        // 表单校验规则
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      roles: [], // 保存返回的结果
      pageParams: {
        // 设置页码显示
        page: 1, // 查询第n页
        pagesize: 3 // 每页n条
      },
      total: 0 // 数据总条数-默认
    }
  },

  computed: {
    // 使用计算属性变更标题名
    cTitle() {
      return this.isEdit ? '编辑' : '新增'
    }
  },

  created() {
    this.loadRoles()
  },

  methods: {

    async doEdit() {
      // 执行编辑
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('编辑成功')
        this.showDialog = false
      } catch (err) {
        this.$message.err('编辑失败')
        console.log(err)
      }
    },

    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },

    hEdit(row) {
      // 点击编辑事件
      this.roleForm = {
        id: row.id,
        name: row.name,
        description: row.description
      }
      this.showDialog = true
      this.isEdit = true
    },

    async doAdd() {
      // 进行添加
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('添加成功')
        this.showDialog = false
      } catch (err) {
        this.$message.err('添加失败')
        console.log(err)
      }
    },

    hSubmit() {
      // 弹层确定事件
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },

    async doDel(id) {
      // 执行删除  加载接口数据
      try {
        const res = await deleteRole(id)
        console.log(res)
        this.$message.success('删除成功')
        if (this.roles.length === 1) {
          this.pageParams.page--
          if (this.roles.length <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
      } catch (err) {
        this.$message.error('删除失败')
      }
    },
    hDel(id) {
      // 删除角色事件
      //   console.log(id)// 要删除的ID
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch((err) => {
          console.log(err)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async loadRoles() {
      // 获取所有角色列表
      try {
        const res = await getRoles(this.pageParams)
        // console.log(res)
        this.roles = res.data.rows // 将返回的数据结果保存
        this.total = res.data.total // 将返回的数据条数保存
      } catch (err) {
        console.log(err)
      }
    },
    currentChangeFn(curPage) {
      // 点击切换页码事件
      //   console.log(curPage)// 点击的页码
      this.pageParams.page = curPage // 当前页码就是点击页码
      this.loadRoles() // 更新 再次加载
    },
    hClose() {
      // 侦听弹窗关闭, 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },

    // 分配权限事件
    hAssign(id) {
      this.roleId = id
      this.showDialogAssign = true
    }
  }
}
</script>

<style>
</style>
